#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import MySQLdb
import sys

import datetime
import time

## Connection aux bases de donnees

db1 = MySQLdb.connect(db = "provencedb", user='maxk69', passwd = "Oups,IveLostMyPassword!")
db2 = MySQLdb.connect(db = "maxk69_marketer", user='maxk69', passwd = "Oups,IveLostMyPassword!")

## Creation des curseurs

cur1 = db1.cursor()
cur3 = db2.cursor()

## Execution de la requete

cur1.execute("SELECT `emailaddress` FROM `em_banned_emails`")

## Premiere iteration

round = cur1.fetchone()

## Boucle sur les resultats

while (round != None):

    ## Ouverture d'un curseur temporaire et test de presence de l'enregistrement

    cur2 = db2.cursor()
    cur2.execute("SELECT * FROM `em_banned_emails` WHERE `emailaddress` = '" + round[0] + "'")
    if (cur2.fetchone() == None):

        ## Ajout de l'enregistrement si inexistant

        cur3.execute("INSERT INTO `em_banned_emails` (emailaddress, list, bandate) VALUES ('" +  round[0] + "', 'g', " + str(time.mktime(datetime.datetime.now().timetuple())) + ")")

    ## Fermeture du curseur temporaire

    cur2.close()

    ## Passage au resultat suivant

    round = cur1.fetchone()

## Fermeture des curseurs

cur1.close()
cur3.close()

## Validation des resultats

db2.commit()

## Sortie

sys.exit(0)
